package com.hlk.hlk_blog.service;

import com.hlk.hlk_blog.dao.BlogMapper;
import com.hlk.hlk_blog.dao.TagMapper;
import com.hlk.hlk_blog.entity.Blog;
import com.hlk.hlk_blog.entity.BlogAndTag;
import com.hlk.hlk_blog.entity.Tag;
import com.hlk.hlk_blog.exception.NotFoundException;
import com.hlk.hlk_blog.util.IDUtils;
import com.hlk.hlk_blog.util.MarkdownUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class BlogserviceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private TagMapper tagMapper;

    //更新浏览数
    @Override
    public void updateViews(Long id) {

    }

    /*-----------后台接口---------------*/
//   获取所有blog
    @Override
    public List<Blog> getAllBlog() {
        return blogMapper.getAllBlog();
    }

    //后台搜索博客
    @Override
    public List<Blog> searchBlog(Blog blog) {
        return blogMapper.searchBlog(blog);
    }

    @Override
    public boolean saveBlog(Blog blog) {
        Long id = Long.parseLong(IDUtils.createID());
        blog.setId(id);
        blog.setCreateTime(new Date());
        blog.setUpdateTime(new Date());
        blog.setViews(0);
        blogMapper.saveBlog(blog);
        //保存博客后才能获取自增的id
        Long id1 = blog.getId();
        //将标签的数据存到t_blogs_tag表中
        List<Tag> tags = blog.getTags();
        BlogAndTag blogAndTag = null;
        for (Tag tag : tags) {
            //新增时无法获取自增的id,在mybatis里修改
            blogAndTag = new BlogAndTag();
            blogAndTag.setBlogId(id1);
            blogAndTag.setTagId(tag.getId());
            blogMapper.saveBlogAndTag(blogAndTag);
        }
        return true;
    }

    /*
     * 修改*/
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateBlog(Blog blog) {
        blog.setUpdateTime(new Date());
        //将标签的数据存到t_blogs_tag表中
        List<Tag> tags = blog.getTags();
        blogMapper.deleteBlogAndTag(blog.getId());
        BlogAndTag blogAndTag = null;
        for (Tag tag : tags) {
            //新增时无法获取自增的id,在mybatis里修改
            blogAndTag = new BlogAndTag();
            blogAndTag.setBlogId(blog.getId());
            blogAndTag.setTagId(tag.getId());
            blogMapper.saveBlogAndTag(blogAndTag);
        }
        if (blog.getRecommend() == null) {
            blog.setRecommend(false);
        }
        if (blog.getCommentabled() == null) {
            blog.setCommentabled(false);
        }
        if (blog.getShareStatement() == null) {
            blog.setShareStatement(false);
        }
        if (blog.getAppreciation() == null) {
            blog.setAppreciation(false);
        }
        return blogMapper.updateBlog(blog);
    }

    /*
     * 删除blog*/
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteBlog(Long id) {
        blogMapper.deleteBlogAndTag(id);
        blogMapper.deleteBlog(id);
        return true;
    }

    @Override
    public Blog getDetailedBlog(Long id) {
        return blogMapper.getDetailedBlog(id);
    }

    @Override
    public List<Blog> listblog() {
        return blogMapper.findAll();
    }

    @Override
    public List<Blog> getAllRecommendBlog(Integer recommendBlogNums) {
        List<Blog> allRecommendBlog = blogMapper.getAllRecommendBlog(recommendBlogNums);
        return allRecommendBlog;
    }

    @Override
    public List<Blog> getBlogByTypeId(Long typeId) {
        return blogMapper.getBlogByTypeId(typeId);
    }

    @Override
    public List<Blog> getBlogByTagId(Long tagId) {
        return blogMapper.getBlogByTagId(tagId);
    }

    @Override
    public List<Blog> newblog(Integer num) {
        return blogMapper.newblog(num);
    }

    @Override
    public Blog findBlogById(Long id) {
        Blog blog = blogMapper.findBlogById(id);
        if (blog == null) {
            throw new NotFoundException("该博客不存在");
        }
        List tags = new ArrayList();
        if (blog.getTagIds() == "" || "".equals(blog.getTagIds())) {

        } else {
            String[] tagIds = blog.getTagIds().split(",");
            for (String tagId : tagIds) {
                Long tagid = Long.parseLong(tagId);
                tags.add(tagMapper.getTagById(tagid));
            }
            blog.setTags(tags);
        }
        String content = blog.getContent();
        blog.setContent(MarkdownUtils.markdownToHtml(content));
        blogMapper.updateViews(id);
        return blog;
    }

    @Override
    public List<String> getblogYear() {
        return blogMapper.getblogYear();
    }

    @Override
    public List<Blog> getBlogByYear(String year) {
        return blogMapper.getBlogByYear(year);
    }

    @Override
    public Long getBlogCount() {
        return blogMapper.getBlogCount();
    }

    @Override
    public List<Blog> searchIndexBlog(String query) {
        return blogMapper.searchIndexBlog(query);
    }


}
